package net.tngou.crawler.drug;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 药房网
 * @author tngou@tngou.net
 *
 */
public class CN_Yaofang extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(CN_Yaofang.class);

	private String url="http://www.yaofang.cn/goods-{}.html";

//	@Override
//	public List<URL> urls() {
//		List<URL> list = new ArrayList<>();
//		for (int i=0;i<SIZE;i++) {	
//			try {
//				URL u = new URL(StringUtil.replace(url, "{}", getId()+""));
//				list.add(u);
//			} catch (MalformedURLException e) {
//				e.printStackTrace();
//			}
//			
//		}
//		return list;
//	}

	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null||doc.text().indexOf("暂不销售")>0)return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		json.put("un",url.toString().split("-")[1].replaceAll(".html", "-").split("-")[0]);
		//标签
		Elements crumbs = doc.getElementsByClass("breadcrumb");
		if(crumbs.first()!=null)
		{
			Elements as = crumbs.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();	
			for (int i = 1; i < as.size(); i++) {	
				list.add(as.get(i).text());
			}
				
			json.put("tag", list);
		}
		//类型 
		Elements info = doc.getElementsByClass("info");
		if(info.first()!=null)
		{
			Elements i = info.first().getElementsByTag("i");
			if(i.first()!=null)
			{
				if(i.first().html().toUpperCase().indexOf("RX")>=0)
				{
					json.put("ptype", Ptype.RX处方药.ordinal());
				}if(i.first().html().toUpperCase().indexOf("OTC")>=0)
				{
					json.put("ptype", Ptype.OTC非处方甲类.ordinal());
					if(i.first().html().toUpperCase().indexOf("乙")>=0)
						json.put("ptype", Ptype.OTC非处方乙类.ordinal());
				}
			}
		}
		//价格
		Element price = doc.getElementById("price");
		if(price!=null)
		{
			String value = price.attr("value");	
			json.put("price", getPrice(value));
			
		}
		//图片
		Elements detail_items = doc.getElementsByClass("detail_items");
		if(detail_items.first()!=null)
		{
			Elements imgs = detail_items.first().getElementsByTag("img");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				String src = e.attr("src");
				if(StringUtil.isNotEmpty(src))
				{
					list.add(src);
				}
			});
			json.put("picture", list);
		}
		//说明书
		Elements instruction = doc.getElementsByClass("instructions");
		if(instruction.first()!=null)
		{
			JSONObject instructions = instructions_tr_td_td(instruction.first().getElementsByTag("table").first());
			if(info.first()!=null)
			instructions = instructions(info.first(),instructions);
			json.put("instructions",instructions );	
		}
		
		return json;
	}
	
	private JSONObject instructions(Element element,JSONObject json) {
		Elements bms = element.getElementsMatchingOwnText("商品编码");
		if(bms.first()!=null)
		{
			String html = bms.first().text();
			html=replaceAll(html);
			String[] sts = StringUtil.split(html, "|");
			for (String e : sts) {
				String[] es = StringUtil.split(e, "：");
				String key = StringUtil.cleanKey(es[0]);
				if(es.length==2)
				{
					json.put(key,StringUtil.RemoveEmpty(es[1]));							
				}
			}
			
		}
		
		return name_number(json);
	}

}
